<html>
<head>
	<title>Programming with SQL Relay using the Python DB-API</title>
	<link rel="stylesheet" href="../css/styles.css">
</head>
<body>
<h1>Programming with SQL Relay using the Python DB-API</h1>

<ul>
  <li><a href="#session">Establishing a Session</a></li>
  <li><a href="#query">Executing Queries</a></li>
  <li><a href="#commit">Commits and Rollbacks</a></li>
  <li><a href="#temptables">Temporary Tables</a></li>
  <li><a href="#errors">Catching Errors</a></li>
  <li><a href="#bindvars">Bind Variables</a></li>
  <li><a href="#rebinding">Re-Binding and Re-Executing</a></li>
  <li><a href="#fields">Accessing Fields in the Result Set</a></li>
  <li><a href="#cursors">Cursors</a></li>
  <li><a href="#columns">Getting Column Information</a></li>
  <li><a href="#storedprocedures">Stored Procedures</a></li>
  <li><a href="#lastinsertid">Getting the Last Insert ID</a></li>
</ul>

<a name="session"/><h2>Establishing a Session</h2>

<p>To use SQL Relay, you have to identify the connection that you intend to 
use.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>from SQLRelay import PySQLRDB

con<font color="#990000">=</font>PySQLRDB<font color="#990000">.</font><b><font color="#000000">connect</font></b><font color="#990000">(</font><font color="#FF0000">'sqlrserver'</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">'/tmp/example.socket'</font><font color="#990000">,</font><font color="#FF0000">'user'</font><font color="#990000">,</font><font color="#FF0000">'password'</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">)</font>
cur<font color="#990000">=</font>con<font color="#990000">.</font><b><font color="#000000">cursor</font></b><font color="#990000">()</font>

<font color="#990000">...</font> execute <font color="#008080">some</font> queries <font color="#990000">...</font>
</tt></pre>

</blockquote>
<p>In addition to the parameters above, the following named parameters can be used to control attributes of the connection.</p>

<ul>
  <li><b>db</b> - The database to switch to when logging into SQL Relay.  May be left empty or omitted to accept the default database that SQL Relay is already connected to.</li>
  <li><b>debug</b> - If this is set to 1 or to a file name then debug is enabled.  If set to a file name then debug will be written to the file.  If set to 1 then debug will be written to the browser.  Defaults to 0.</li>
  <li><b>lazyconnect</b> - By default, the SQL Relay client does "lazy connects".  I.e. the connection to the SQL Relay server isn't established until the first query or other operation is run.  This optimizes availability of the connection pool but is inconstent with most other database API's.  Setting this parameter to 0 causes the connection to the SQL Relay server to be established immediately and the constructor to fail, returning 0, if the connection cannot be established.  Defaults to 1.</li>
</ul>

<p>In addition to the paramters above, the following named parameters can be used to establish Kerberos or Active Directory encryption and authentication with the server:</p>

<p>See the <a href="../admin/configguide.html#krb">SQL Relay Configuration Guide</a> for more information about Kerberos and Active Directory configurations.  In particular, user and password are not typically used when using Kerberos/AD.</p>

<ul>
  <li><b>krb</b> - Set to <b>yes</b> to enable Kerberos/AD encryption/authentication or <b>no</b> to disable it.</li>
  <li><b>krbservice</b> - Specifies the Kerberos service name of the SQL Relay server.  If omitted or left empty then the service name "sqlrelay" will be used. "sqlrelay" is the default service name of the SQL Relay server.  Note that on Windows platforms the service name must be fully qualified, including the host and realm name.  For example: "sqlrelay/sqlrserver.firstworks.com@AD.FIRSTWORKS.COM".</li>
  <li><b>krbmech</b> - Specifies the Kerberos mechanism to use.  On Linux/Unix platforms, this should be a string representation of the mechnaism's OID, such as: { 1 2 840 113554 1 2 2 }.  On Windows platforms, this should be a string like: Kerberos.  If omitted or left empty then the default mechanism will be used.  Only set this if you know that you have a good reason to.</li>
  <li><b>krbflags</b> - Specifies what Kerberos flags to use.  Multiple flags may be specified, separated by commas.  If left empty or NULL then a defalt set of flags will be used.  Only set this if you know that you have a good reason to.  Valid flags include: GSS_C_MUTUAL_FLAG, GSS_C_REPLAY_FLAG, GSS_C_SEQUENCE_FLAG, GSS_C_CONF_FLAG, and GSS_C_INTEG_FLAG.  For a full list of flags, consult the GSSAPI documentation, though note that only the previously listed flags are supported on Windows.</li>
</ul>

<p>In addition to the parameters above, the following named parameters can be used to establish TLS/SSL encryption and authentication with the server:</p>

<p>See the <a href="../admin/configguide.html#tls">SQL Relay Configuration Guide</a> for more information about TLS/SSL configurations.</p>

<ul>
  <li><b>tls</b> - Set to <b>yes</b> to enable TLS/SSL encryption/authentication or <b>no</b> to disable it.</li>
  <li><b>tlsversion</b> - Specifies the TLS/SSL protocol version that the client will attempt to use.  Valid values include SSL2, SSL3, TLS1, TLS1.1, TLS1.2 or any more recent version of TLS, as supported by and enabled in the underlying TLS/SSL library.  If omittted or left empty then the highest supported version will be negotiated.</li>
  <li><b>tlscert</b> - Specifies the file name of the certificate chain file to send to the SQL Relay server.  This is only necessary if the SQL Relay server is configured to authenticate and authorize clients by certificate.</li>
  <li><b>tlspassword</b> - If tlscert contains a password-protected private key, then tlspassword may be supplied to access it.  If the private key is not password-protected, then this argument is ignored, and may be omitted or left empty.</li>
  <li><b>tlsciphers</b> - Specifies a list of ciphers to allow.  Ciphers may be separated by spaces, commas, or colons.  If omitted or left empty then a default set is used.  For a list of valid ciphers on Linux/Unix platforms, see: man ciphers. For a list of valid ciphers on Windows platforms, see <a target="_blank" href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa375549%28v=vs.85%29.aspx">this page</a>.  On Windows platforms, the ciphers (alg_id's) should omit CALG_ and may be given with underscores or dashes.  For example: 3DES_112.  Only set this if you know that you have a good reason to.</li>
  <li><b>tlsvalidate</b> - Indicates whether to validate the SQL Relay server's certificate, and may be set to one of the following: <b>no</b> - Don't validate the server's certificate.  <b>ca</b> - Validate that the server's certificate was signed by a trusted certificate authority.  <b>ca+host</b> - Perform "ca" validation and also validate that one of the subject altenate names (or the common name if no SANs are present) in the certificate matches the host parameter.  (Falls back to "ca" validation when a unix socket is used.)  <b>ca+domain</b> - Perform "ca" validation and also validate that the domain name of one of the subject alternate names (or the common name if no SANs are present) in the certificate matches the domain name of the host parameter.  (Falls back to "ca" validation when a unix socket is used.)</li>
  <li><b>tlsca</b> - Specifies the location of a certificate authority file to use, in addition to the system's root certificates, when validating the SQL Relay server's certificate.  This is useful if the SQL Relay server's certificate is self-signed.  On Windows, "ca" must be a file name.  On non-Windows systems, "ca" can be either a file or  directory name.  If it is a directory name, then all certificate authority files found in that directory will be used.  If it a file name, then only that file will be used.</li>
</ul>

<p>Note that the supported tlscert and tlsca file formats may vary between platforms.  A variety of file formats are generally supported on Linux/Unix platfoms (.pem, .pfx, etc.) but only the .pfx format is currently supported on Windows.</p>

<p>In addition to the paramters above, the following named parameters can be used to control the default statement attributes:</p>

<ul>
  <li><b>resultsetbuffersize</b> - Sets the number of rows to buffer at a time.  See <a href="#largeresultsets">Dealing With Large Result Sets</a> below.  If set to 0 (the default) then the entire result set is buffered.</li>
  <li><b>columnnamecase</b> - Indicates the case to convert column names to.  May be set to "mixed", "upper", or "lower".  Defaults to "mixed".</li>
  <li><b>dontgetcolumninfo</b> - If this is set to 1 then column info is not fetched with the result set.  If set to 0 then column info is fetched.  Defaults to 0.</li>
  <li><b>nullsasnulls</b> - If this is set to 1 then NULL values are returned as nulls.  If set to 0 then NULL values are returned as empty strings.  Defaults to 0.</li>
</ul>

<p>After calling connect(), a session is established when the first 
query or other operation is run, unless lazyconnect=0 is used, in which case
a session is established immediately.</p>

<p>For the duration of the session, the client occupies one of the database
connections, so care should be taken to minimize the length of a
session.</p>

<a name="query"/><h2>Executing Queries</h2>

<p>Call execute() to run a query.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>from SQLRelay import PySQLRDB

con<font color="#990000">=</font>PySQLRDB<font color="#990000">.</font><b><font color="#000000">connect</font></b><font color="#990000">(</font><font color="#FF0000">'sqlrserver'</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">'/tmp/example.socket'</font><font color="#990000">,</font><font color="#FF0000">'user'</font><font color="#990000">,</font><font color="#FF0000">'password'</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">)</font>
cur<font color="#990000">=</font>con<font color="#990000">.</font><b><font color="#000000">cursor</font></b><font color="#990000">()</font>

cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'select * from my_table'</font><font color="#990000">)</font>

<font color="#990000">...</font> process the <font color="#008080">result</font> set <font color="#990000">...</font>

</tt></pre>

</blockquote>
<a name="commit"/><h2>Commits and Rollbacks</h2>

<p>If you need to execute a commit or rollback, you should use the commit()
and rollback() methods rather than sending a "commit" or "rollback" query.  
There are two reasons for this.  First, it's much more efficient to call the 
methods.  Second, if you're writing code that can run on transactional or 
non-transactional databases, some non-transactional databases will throw errors
if they receive a "commit" or "rollback" query, but by calling the commit() 
and rollback() methods you instruct the database connection daemon to call the 
commit and rollback API methods for that database rather than issuing them as 
queries.  If the API's have no commit or rollback methods, the calls do nothing
and the database throws no error.</p>

<a name="temptables"/><h2>Temporary Tables</h2>

<p>Some databases support temporary tables.  That is, tables which are
automatically dropped or truncated when an application closes its connection
to the database or when a transaction is committed or rolled back.</p>

<p>For databases which drop or truncate tables when a transaction is committed
or rolled back, temporary tables work naturally.</p>

<p>However, for databases which drop or truncate tables when an application
closes its connection to the database, there is an issue.  Since SQL Relay
maintains persistent database connections, when an application disconnects from
SQL Relay, the connection between SQL Relay and the database remains, so
the database does not know to drop or truncate the table.  To remedy this
situation, SQL Relay parses each query to see if it created a temporary table,
keeps a list of temporary tables and drops (or truncates them) when the
application disconnects from SQL Relay.  Since each database has slightly
different syntax for creating a temporary table, SQL Relay parses each query
according to the rules for that database.</p>

<p>In effect, temporary tables should work when an application connects to
SQL Relay in the same manner that they would work if the application connected
directly to the database.</p>

<a name="errors"/><h2>Catching Errors</h2>

<p>If your call to execute() raises an exception, the query failed.
You can find out why by catching the exception.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>from SQLRelay import PySQLRDB

con<font color="#990000">=</font>PySQLRDB<font color="#990000">.</font><b><font color="#000000">connect</font></b><font color="#990000">(</font><font color="#FF0000">'sqlrserver'</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">'/tmp/example.socket'</font><font color="#990000">,</font><font color="#FF0000">'user'</font><font color="#990000">,</font><font color="#FF0000">'password'</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">)</font>
cur<font color="#990000">=</font>con<font color="#990000">.</font><b><font color="#000000">cursor</font></b><font color="#990000">()</font>

<b><font color="#0000FF">try</font></b><font color="#990000">:</font>
	cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'select * from my_nonexistant_table'</font><font color="#990000">)</font>
<font color="#008080">except</font> PySQLRDB<font color="#990000">.</font>DatabaseError<font color="#990000">,</font> e<font color="#990000">:</font>
	print e
</tt></pre>

</blockquote>
<a name="bindvars"/><h2>Bind Variables</h2>

<p>Programs rarely execute fixed queries.  More often than not, some part
of the query is dynamically generated.  The Python DB-API provides a means
for using bind variables in those queries.</p>

<p>For a detailed discussion of binds, see 
<a href="binds.html">this document</a>.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>from SQLRelay import PySQLRDB

con<font color="#990000">=</font>PySQLRDB<font color="#990000">.</font><b><font color="#000000">connect</font></b><font color="#990000">(</font><font color="#FF0000">'sqlrserver'</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">'/tmp/example.socket'</font><font color="#990000">,</font><font color="#FF0000">'user'</font><font color="#990000">,</font><font color="#FF0000">'password'</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">)</font>
cur<font color="#990000">=</font>con<font color="#990000">.</font><b><font color="#000000">cursor</font></b><font color="#990000">()</font>

cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'select * from my_table where column1&gt;:val1 and column2=:val2 and column3&lt;:val3'</font><font color="#990000">,</font><font color="#FF0000">{</font><font color="#FF0000">'val1'</font><font color="#990000">:</font><font color="#993399">1</font><font color="#990000">,</font><font color="#FF0000">'val2'</font><font color="#990000">:</font><font color="#FF0000">'hello'</font><font color="#990000">,</font><font color="#FF0000">'val3'</font><font color="#990000">:</font><font color="#993399">50.546</font><font color="#FF0000">}</font><font color="#990000">)</font>

<font color="#990000">...</font> process the <font color="#008080">result</font> set <font color="#990000">...</font>
</tt></pre>

</blockquote>
<p>When passing a floating point number in as a bind or substitution variable,
you have to supply precision and scale for the number.  See
<a href="precisionscale.html">this page</a> for a discussion of precision and
scale.</p>

<a name="rebinding"/><h2>Re-Binding and Re-Execution</h2>

<p>A feature of the prepare/bind/execute paradigm is the ability to prepare, 
bind and execute a query once, then re-bind and re-execute the query 
over and over without re-preparing it.  If your backend database natively 
supports this paradigm, you can reap a substantial performance improvement.</p>

<p>The Python DB-API supports this paradigm via the executemany method.  If you
pass in a list of parameter dictionaries, the query will be re-executed for
each dictionary of bind variable/values.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>from SQLRelay import PySQLRDB

con<font color="#990000">=</font>PySQLRDB<font color="#990000">.</font><b><font color="#000000">connect</font></b><font color="#990000">(</font><font color="#FF0000">'sqlrserver'</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">'/tmp/example.socket'</font><font color="#990000">,</font><font color="#FF0000">'user'</font><font color="#990000">,</font><font color="#FF0000">'password'</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">)</font>
cur<font color="#990000">=</font>con<font color="#990000">.</font><b><font color="#000000">cursor</font></b><font color="#990000">()</font>

cur<font color="#990000">.</font><b><font color="#000000">executemany</font></b><font color="#990000">(</font><font color="#FF0000">'insert into my_table values (:val1,:val2,:val3)'</font><font color="#990000">,</font>
		<font color="#990000">[</font><font color="#FF0000">{</font><font color="#FF0000">'val1'</font><font color="#990000">:</font><font color="#993399">1</font><font color="#990000">,</font><font color="#FF0000">'val2'</font><font color="#990000">:</font><font color="#FF0000">'hello'</font><font color="#990000">,</font><font color="#FF0000">'val3'</font><font color="#990000">:</font><font color="#993399">1.11</font><font color="#FF0000">}</font><font color="#990000">,</font>
		<font color="#FF0000">{</font><font color="#FF0000">'val1'</font><font color="#990000">:</font><font color="#993399">2</font><font color="#990000">,</font><font color="#FF0000">'val2'</font><font color="#990000">:</font><font color="#FF0000">'hi'</font><font color="#990000">,</font><font color="#FF0000">'val3'</font><font color="#990000">:</font><font color="#993399">2.22</font><font color="#FF0000">}</font><font color="#990000">,</font>
		<font color="#FF0000">{</font><font color="#FF0000">'val1'</font><font color="#990000">:</font><font color="#993399">3</font><font color="#990000">,</font><font color="#FF0000">'val2'</font><font color="#990000">:</font><font color="#FF0000">'bye'</font><font color="#990000">,</font><font color="#FF0000">'val3'</font><font color="#990000">:</font><font color="#993399">3</font><font color="#990000">,</font><font color="#993399">33</font><font color="#FF0000">}</font><font color="#990000">])</font>
</tt></pre>

</blockquote>
<a name="fields"/><h2>Accessing Fields in the Result Set</h2>

<p>The fetchone(), fetchmany() and fetchall() methods are useful for processing
result sets.  fetchone() returns a list of values.  fetchmany() and fetchall()
each return a list of rows where each row is a list of values.</p>

<p>The rowcount member variable gives the number of rows in the result set of
a select query or the number of rows affected by an insert/update/delete query.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>from SQLRelay import PySQLRDB

con<font color="#990000">=</font>PySQLRDB<font color="#990000">.</font><b><font color="#000000">connect</font></b><font color="#990000">(</font><font color="#FF0000">'sqlrserver'</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">'/tmp/example.socket'</font><font color="#990000">,</font><font color="#FF0000">'user'</font><font color="#990000">,</font><font color="#FF0000">'password'</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">)</font>
cur<font color="#990000">=</font>con<font color="#990000">.</font><b><font color="#000000">cursor</font></b><font color="#990000">()</font>

cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'select * from my_table'</font><font color="#990000">)</font>

print <font color="#FF0000">'rowcount:'</font><font color="#990000">,</font> cur<font color="#990000">.</font>rowcount

print <font color="#FF0000">'the first row:'</font>
<font color="#008080">print</font> cur<font color="#990000">.</font><b><font color="#000000">fetchone</font></b><font color="#990000">()</font>
print

print <font color="#FF0000">'the next three rows:'</font>
<font color="#008080">print</font> cur<font color="#990000">.</font><b><font color="#000000">fetchmany</font></b><font color="#990000">(</font><font color="#993399">3</font><font color="#990000">)</font>
print

print <font color="#FF0000">'the rest of the rows:'</font>
<font color="#008080">print</font> cur<font color="#990000">.</font><b><font color="#000000">fetchall</font></b><font color="#990000">()</font>
print
</tt></pre>

</blockquote>
<a name="cursors"/><h2>Cursors</h2>

<p>Cursors make it possible to execute queries while processing the result
set of another query.  You can select rows from a table in one query, then 
iterate through its result set, inserting rows into another table, using only 
1 database connection for both operations.</p>

<p>For example:</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>from SQLRelay import PySQLRDB

con<font color="#990000">=</font>PySQLRDB<font color="#990000">.</font><b><font color="#000000">connect</font></b><font color="#990000">(</font><font color="#FF0000">'sqlrserver'</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">'/tmp/example.socket'</font><font color="#990000">,</font><font color="#FF0000">'user'</font><font color="#990000">,</font><font color="#FF0000">'password'</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">)</font>
cursor1<font color="#990000">=</font>con<font color="#990000">.</font><b><font color="#000000">cursor</font></b><font color="#990000">()</font>
cursor2<font color="#990000">=</font>con<font color="#990000">.</font><b><font color="#000000">cursor</font></b><font color="#990000">()</font>

cursor1<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'select * from my_huge_table'</font><font color="#990000">)</font>

<b><font color="#0000FF">for</font></b> a <font color="#008080">in</font> cursor1<font color="#990000">.</font><b><font color="#000000">fetchall</font></b><font color="#990000">():</font>
        cursor2<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'insert into my_other_table values (:1,:2,:3)'</font><font color="#990000">,</font><font color="#FF0000">{</font><font color="#FF0000">':1'</font><font color="#990000">,</font>a<font color="#990000">[</font><font color="#993399">0</font><font color="#990000">],</font><font color="#FF0000">':2'</font><font color="#990000">,</font>a<font color="#990000">[</font><font color="#993399">1</font><font color="#990000">],</font><font color="#FF0000">':3'</font><font color="#990000">,</font>a<font color="#990000">[</font><font color="#993399">2</font><font color="#990000">]</font><font color="#FF0000">}</font><font color="#990000">)</font>
</tt></pre>

</blockquote>
<a name="columns"/><h2>Getting Column Information</h2>

<p>After executing a query, column information is stored in the desc 
variable.  desc is a list of tuples.  Each tuple corresponds to a column, 
containing its name, type and length. </p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>from SQLRelay import PySQLRDB

con<font color="#990000">=</font>PySQLRDB<font color="#990000">.</font><b><font color="#000000">connect</font></b><font color="#990000">(</font><font color="#FF0000">'sqlrserver'</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">'/tmp/example.socket'</font><font color="#990000">,</font><font color="#FF0000">'user'</font><font color="#990000">,</font><font color="#FF0000">'password'</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">)</font>
cur<font color="#990000">=</font>con<font color="#990000">.</font><b><font color="#000000">cursor</font></b><font color="#990000">()</font>

cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'select * from my_table'</font><font color="#990000">)</font>

<b><font color="#0000FF">for</font></b> name<font color="#990000">,</font>type<font color="#990000">,</font>length <font color="#008080">in</font> cur<font color="#990000">.</font>desc<font color="#990000">:</font>
        print <font color="#FF0000">'Name:          '</font><font color="#990000">,</font> name
        print <font color="#FF0000">'Type:          '</font><font color="#990000">,</font> type
        print <font color="#FF0000">'Length:        '</font><font color="#990000">,</font> length
</tt></pre>

</blockquote>
<br/><a name="storedprocedures"/><h2>Stored Procedures</h2>

<p>Many databases support stored procedures.  Stored procedures are sets of
queries and procedural code that are executed inside of the database itself.
For example, a stored procedure may select rows from one table, iterate through
the result set and, based on the values in each row, insert, update or delete
rows in other tables.  A client program could do this as well, but a stored
procedure is generally more efficient because queries and result sets don't
have to be sent back and forth between the client and database.  Also, stored
procedures are generally stored in the database in a compiled state, while
queries may have to be re-parsed and re-compiled each time they are sent.</p>

<p>While many databases support stored procedures.  The syntax for creating
and executing stored procedures varies greatly between databases.</p>

<p>Stored procedures typically take input paramters from client programs through
input bind variables and return values back to client programs either through
bind variables or result sets.  Stored procedures can be broken down into
several categories, based on the values that they return.  Some stored
procedures don't return any values, some return a single value, some return
multiple values and some return entire result sets.</p>

<h3>No Values</h3>

<p>Some stored procedures don't return any values.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<h4>Oracle</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font>in1 <font color="#008080">in</font> number<font color="#990000">,</font> in2 <font color="#008080">in</font> number<font color="#990000">,</font> in3 <font color="#008080">in</font> varchar2<font color="#990000">)</font> is
begin
        insert into <font color="#008080">mytable</font> <b><font color="#000000">values</font></b> <font color="#990000">(</font>in1<font color="#990000">,</font>in2<font color="#990000">,</font>in3<font color="#990000">);</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'begin exampleproc(:in1,:in2,:in3); end;'</font><font color="#990000">,</font><font color="#FF0000">{</font><font color="#FF0000">'in1'</font><font color="#990000">:</font><font color="#993399">1</font><font color="#990000">,</font><font color="#FF0000">'in2'</font><font color="#990000">:</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#FF0000">'in3'</font><font color="#990000">:</font><font color="#FF0000">'hello'</font><font color="#FF0000">}</font><font color="#990000">)</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<br/><h4>Sybase and Microsoft SQL Server</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> exampleproc @<font color="#008080">in1</font> <font color="#009900">int</font><font color="#990000">,</font> @<font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> @<font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">)</font> as
        insert into <font color="#008080">mytable</font> <b><font color="#000000">values</font></b> <font color="#990000">(</font>@in1<font color="#990000">,</font>@in2<font color="#990000">,</font>@in3<font color="#990000">)</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'exec exampleproc'</font><font color="#990000">,</font><font color="#FF0000">{</font><font color="#FF0000">'in1'</font><font color="#990000">:</font><font color="#993399">1</font><font color="#990000">,</font><font color="#FF0000">'in2'</font><font color="#990000">:</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#FF0000">'in3'</font><font color="#990000">:</font><font color="#FF0000">'hello'</font><font color="#FF0000">}</font><font color="#990000">)</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<h4>Firebird</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font><font color="#008080">in1</font> integer<font color="#990000">,</font> <font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> as
begin
        insert into <font color="#008080">mytable</font> <b><font color="#000000">values</font></b> <font color="#990000">(</font>in1<font color="#990000">,</font>in2<font color="#990000">,</font>in3<font color="#990000">);</font>
        suspend<font color="#990000">;</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'exec procedure exampleproc ?, ?, ?'</font><font color="#990000">,</font><font color="#FF0000">{</font><font color="#FF0000">'in1'</font><font color="#990000">:</font><font color="#993399">1</font><font color="#990000">,</font><font color="#FF0000">'in2'</font><font color="#990000">:</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#FF0000">'in3'</font><font color="#990000">:</font><font color="#FF0000">'hello'</font><font color="#FF0000">}</font><font color="#990000">)</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<h4>DB2</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font>in <font color="#008080">in1</font> <font color="#009900">int</font><font color="#990000">,</font> in <font color="#008080">in2</font> <font color="#009900">double</font><font color="#990000">,</font> in <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> language sql
begin
        insert into <font color="#008080">mytable</font> <b><font color="#000000">values</font></b> <font color="#990000">(</font>in1<font color="#990000">,</font>in2<font color="#990000">,</font>in3<font color="#990000">);</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'call exampleproc(?,?,?)'</font><font color="#990000">,</font><font color="#FF0000">{</font><font color="#FF0000">'in1'</font><font color="#990000">:</font><font color="#993399">1</font><font color="#990000">,</font><font color="#FF0000">'in2'</font><font color="#990000">:</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#FF0000">'in3'</font><font color="#990000">:</font><font color="#FF0000">'hello'</font><font color="#FF0000">}</font><font color="#990000">)</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<h4>Postgresql</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">function</font> <b><font color="#000000">examplefunc</font></b><font color="#990000">(</font><font color="#009900">int</font><font color="#990000">,</font><font color="#009900">float</font><font color="#990000">,</font><b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> returns <font color="#008080">void</font> as <font color="#FF0000">'</font>
<font color="#FF0000">begin</font>
<font color="#FF0000">        insert into mytable values ($1,$2,$3);</font>
<font color="#FF0000">        return;</font>
<font color="#FF0000">end;'</font> language plpgsql
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'select examplefunc($1,$2,$3)'</font><font color="#990000">,</font><font color="#FF0000">{</font><font color="#FF0000">'1'</font><font color="#990000">:</font><font color="#993399">1</font><font color="#990000">,</font><font color="#FF0000">'2'</font><font color="#990000">:</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#FF0000">'3'</font><font color="#990000">:</font><font color="#FF0000">'hello'</font><font color="#FF0000">}</font><font color="#990000">)</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop <font color="#008080">function</font> <b><font color="#000000">examplefunc</font></b><font color="#990000">(</font><font color="#009900">int</font><font color="#990000">,</font><font color="#009900">float</font><font color="#990000">,</font><b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font>
</tt></pre>

</blockquote>
<h4>MySQL/MariaDB</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font>in <font color="#008080">in1</font> <font color="#009900">int</font><font color="#990000">,</font> in <font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> in <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font>
begin
        insert into <font color="#008080">mytable</font> <b><font color="#000000">values</font></b> <font color="#990000">(</font>in1<font color="#990000">,</font>in2<font color="#990000">,</font>in3<font color="#990000">);</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'call exampleproc(?,?,?)'</font><font color="#990000">,</font><font color="#FF0000">{</font><font color="#FF0000">'1'</font><font color="#990000">:</font><font color="#993399">1</font><font color="#990000">,</font><font color="#FF0000">'2'</font><font color="#990000">:</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#FF0000">'3'</font><font color="#990000">:</font><font color="#FF0000">'hello'</font><font color="#FF0000">}</font><font color="#990000">)</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<br/><h3>Single Values</h3>

<p>Some stored procedures return single values.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<h4>Oracle</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">function</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font>in1 <font color="#008080">in</font> number<font color="#990000">,</font> in2 <font color="#008080">in</font> number<font color="#990000">,</font> in3 <font color="#008080">in</font> varchar2<font color="#990000">)</font> returns number is
begin
        <b><font color="#0000FF">return</font></b> in1<font color="#990000">;</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'select exampleproc(:in1,:in2,:in3) from dual'</font><font color="#990000">,</font><font color="#FF0000">{</font><font color="#FF0000">'in1'</font><font color="#990000">:</font><font color="#993399">1</font><font color="#990000">,</font><font color="#FF0000">'in2'</font><font color="#990000">:</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#FF0000">'in3'</font><font color="#990000">:</font><font color="#FF0000">'hello'</font><font color="#FF0000">}</font><font color="#990000">)</font>
result<font color="#990000">=</font>cur<font color="#990000">.</font><b><font color="#000000">fetchone</font></b><font color="#990000">()[</font><font color="#993399">0</font><font color="#990000">]</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<h4>Sybase and Microsoft SQL Server</h4>

<p>In Sybase and Microsoft SQL Server, stored procedures return values through output parameters rather than as return values of the procedure itself. However, the SQL Relay Python DB-API driver does not currently support output parameters.</p>

<h4>Firebird</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font><font color="#008080">in1</font> integer<font color="#990000">,</font> <font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> <b><font color="#000000">returns</font></b> <font color="#990000">(</font><font color="#008080">out1</font> integer<font color="#990000">)</font> as
begin
        out1<font color="#990000">=</font>in1<font color="#990000">;</font>
        suspend<font color="#990000">;</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'select * from exampleproc(?,?,?)'</font><font color="#990000">,</font><font color="#FF0000">{</font><font color="#FF0000">'in1'</font><font color="#990000">:</font><font color="#993399">1</font><font color="#990000">,</font><font color="#FF0000">'in2'</font><font color="#990000">:</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#FF0000">'in3'</font><font color="#990000">:</font><font color="#FF0000">'hello'</font><font color="#FF0000">}</font><font color="#990000">)</font>
result<font color="#990000">=</font>cur<font color="#990000">.</font><b><font color="#000000">fetchone</font></b><font color="#990000">()[</font><font color="#993399">0</font><font color="#990000">]</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<h4>DB2</h4>

<p>In DB2, stored procedures return values through output parameters rather than as return values of the procedure itself. However, the SQL Relay Python DB driver does not currently support output parameters.</p>

<h4>Postgresql</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">function</font> <b><font color="#000000">examplefunc</font></b><font color="#990000">(</font><font color="#009900">int</font><font color="#990000">,</font><font color="#009900">float</font><font color="#990000">,</font><font color="#009900">char</font><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> returns <font color="#008080">int</font> as <font color="#FF0000">'</font>
<font color="#FF0000">declare</font>
<font color="#FF0000">        in1 int;</font>
<font color="#FF0000">        in2 float;</font>
<font color="#FF0000">        in3 char(20);</font>
<font color="#FF0000">begin</font>
<font color="#FF0000">        in1:=$1;</font>
<font color="#FF0000">        return;</font>
<font color="#FF0000">end;</font>
<font color="#FF0000">'</font> language plpgsql
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'select * from examplefunc($1,$2,$3)'</font><font color="#990000">,</font><font color="#FF0000">{</font><font color="#FF0000">'1'</font><font color="#990000">:</font><font color="#993399">1</font><font color="#990000">,</font><font color="#FF0000">'2'</font><font color="#990000">:</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#FF0000">'3'</font><font color="#990000">:</font><font color="#FF0000">'hello'</font><font color="#FF0000">}</font><font color="#990000">)</font>
result<font color="#990000">=</font>cur<font color="#990000">.</font><b><font color="#000000">fetchone</font></b><font color="#990000">()[</font><font color="#993399">0</font><font color="#990000">]</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop <font color="#008080">function</font> <b><font color="#000000">examplefunc</font></b><font color="#990000">(</font><font color="#009900">int</font><font color="#990000">,</font><font color="#009900">float</font><font color="#990000">,</font><font color="#009900">char</font><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font>
</tt></pre>

</blockquote>
<h4>MySQL/MariaDB</h4>

<p>A single value can be returned from a MySQL/MariaDB function.</p>

<p>To create the function, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">function</font> <b><font color="#000000">examplefunc</font></b><font color="#990000">(</font>in <font color="#008080">in1</font> <font color="#009900">int</font><font color="#990000">,</font> in <font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> in <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> returns <font color="#009900">int</font> <b><font color="#0000FF">return</font></b> in1<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the function from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'select examplefunc(?,?,?)'</font><font color="#990000">,</font><font color="#FF0000">{</font><font color="#FF0000">'in1'</font><font color="#990000">:</font><font color="#993399">1</font><font color="#990000">,</font><font color="#FF0000">'in2'</font><font color="#990000">:</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#FF0000">'in3'</font><font color="#990000">:</font><font color="#FF0000">'hello'</font><font color="#FF0000">}</font><font color="#990000">)</font>
</tt></pre>

</blockquote>
<p>To drop the function, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<p>A single value can be returned in the result set of a MySQL/MariaDB procedure.</p>

<p>To create the procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">()</font> begin select <font color="#993399">1</font><font color="#990000">;</font> end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execeute the procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'select examplefunc()'</font><font color="#990000">)</font>
result<font color="#990000">=</font>cur<font color="#990000">.</font><b><font color="#000000">fetchone</font></b><font color="#990000">()[</font><font color="#993399">0</font><font color="#990000">]</font>
</tt></pre>

</blockquote>
<p>To drop the procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<p>A single value can be returned using the output variable of a MySQL/MariaDB
procedure.</p>

<p>To create the procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font>out <font color="#008080">out1</font> <font color="#009900">int</font><font color="#990000">)</font> begin select <font color="#993399">1</font> <font color="#008080">into</font> out1<font color="#990000">;</font> end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execeute the procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'set @out1=0'</font><font color="#990000">)</font>
cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'call exampleproc()'</font><font color="#990000">)</font>
cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'select @out1'</font><font color="#990000">)</font>
result<font color="#990000">=</font>cur<font color="#990000">.</font><b><font color="#000000">fetchone</font></b><font color="#990000">()[</font><font color="#993399">0</font><font color="#990000">]</font>
</tt></pre>

</blockquote>
<p>To drop the procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<br/><h3>Multiple Values</h3>

<p>Some stored procedures return multiple values.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<h4>Oracle</h4>

<p>In Oracle, stored procedures can return values through output parameters or as return values of the procedure itself. If a procedure needs to return multiple values, it can return one of them as the return value of the procedure itself, but the rest must be returned through output parameters. However, the SQL Relay Python DB-API driver does not currently support output parameters.</p>

<h4>Sybase and Microsoft SQL Server</h4>

<p>In Sybase and Microsoft SQL Server, stored procedures return values through output parameters rather than as return values of the procedure itself. However, the SQL Relay Python DB-API driver does not currently support output parameters.</p>

<h4>Firebird</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font><font color="#008080">in1</font> integer<font color="#990000">,</font> <font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> <b><font color="#000000">returns</font></b> <font color="#990000">(</font><font color="#008080">out1</font> integer<font color="#990000">,</font> <font color="#008080">out2</font> <font color="#009900">float</font><font color="#990000">,</font> <font color="#008080">out3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> as
begin
        out1<font color="#990000">=</font>in1<font color="#990000">;</font>
        out2<font color="#990000">=</font>in2<font color="#990000">;</font>
        out3<font color="#990000">=</font>in3<font color="#990000">;</font>
        suspend<font color="#990000">;</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'select * from examplefunc(?,?,?)'</font><font color="#990000">,</font><font color="#FF0000">{</font><font color="#FF0000">'in1'</font><font color="#990000">:</font><font color="#993399">1</font><font color="#990000">,</font><font color="#FF0000">'in2'</font><font color="#990000">:</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#FF0000">'in3'</font><font color="#990000">:</font><font color="#FF0000">'hello'</font><font color="#FF0000">}</font><font color="#990000">)</font>
out1<font color="#990000">=</font>cur<font color="#990000">.</font><b><font color="#000000">fetchone</font></b><font color="#990000">()[</font><font color="#993399">0</font><font color="#990000">]</font>
out2<font color="#990000">=</font>cur<font color="#990000">.</font><b><font color="#000000">fetchone</font></b><font color="#990000">()[</font><font color="#993399">1</font><font color="#990000">]</font>
out3<font color="#990000">=</font>cur<font color="#990000">.</font><b><font color="#000000">fetchone</font></b><font color="#990000">()[</font><font color="#993399">2</font><font color="#990000">]</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<h4>DB2</h4>

<p>In DB2, stored procedures return values through output parameters rather than as return values of the procedure itself. However, the SQL Relay Python DB driver does not currently support output parameters.</p>

<h4>Postgresql</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">function</font> <b><font color="#000000">examplefunc</font></b><font color="#990000">(</font><font color="#009900">int</font><font color="#990000">,</font><font color="#009900">float</font><font color="#990000">,</font><font color="#009900">char</font><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> returns <font color="#008080">record</font> as <font color="#FF0000">'</font>
<font color="#FF0000">declare</font>
<font color="#FF0000">        output record;</font>
<font color="#FF0000">begin</font>
<font color="#FF0000">        select $1,$2,$3 into output;</font>
<font color="#FF0000">        return output;</font>
<font color="#FF0000">end;</font>
<font color="#FF0000">'</font> language plpgsql
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'select * from examplefunc($1,$2,$3) as (col1 int, col2 float, col3 char(20))'</font><font color="#990000">,</font><font color="#FF0000">{</font><font color="#FF0000">'1'</font><font color="#990000">:</font><font color="#993399">1</font><font color="#990000">,</font><font color="#FF0000">'2'</font><font color="#990000">:</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#FF0000">'3'</font><font color="#990000">:</font><font color="#FF0000">'hello'</font><font color="#FF0000">}</font><font color="#990000">)</font>
out1<font color="#990000">=</font>cur<font color="#990000">.</font><b><font color="#000000">fetchone</font></b><font color="#990000">()[</font><font color="#993399">0</font><font color="#990000">]</font>
out2<font color="#990000">=</font>cur<font color="#990000">.</font><b><font color="#000000">fetchone</font></b><font color="#990000">()[</font><font color="#993399">1</font><font color="#990000">]</font>
out3<font color="#990000">=</font>cur<font color="#990000">.</font><b><font color="#000000">fetchone</font></b><font color="#990000">()[</font><font color="#993399">2</font><font color="#990000">]</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop <font color="#008080">function</font> <b><font color="#000000">examplefunc</font></b><font color="#990000">(</font><font color="#009900">int</font><font color="#990000">,</font><font color="#009900">float</font><font color="#990000">,</font><font color="#009900">char</font><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font>
</tt></pre>

</blockquote>
<h4>MySQL/MariaDB</h4>

<p>Here's how you can get multiple values from the result
set of a MySQL/MariaDB procedure.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font>in <font color="#008080">in1</font> <font color="#009900">int</font><font color="#990000">,</font> in <font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> in <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> begin <font color="#008080">select</font> in1<font color="#990000">,</font> in2<font color="#990000">,</font> in3<font color="#990000">;</font> end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'select examplefunc(?,?,?)'</font><font color="#990000">,</font><font color="#FF0000">{</font><font color="#FF0000">'1'</font><font color="#990000">:</font><font color="#993399">1</font><font color="#990000">,</font><font color="#FF0000">'2'</font><font color="#990000">:</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#FF0000">'3'</font><font color="#990000">:</font><font color="#FF0000">'hello'</font><font color="#FF0000">}</font><font color="#990000">)</font>
out1<font color="#990000">=</font>cur<font color="#990000">.</font><b><font color="#000000">fetchone</font></b><font color="#990000">()[</font><font color="#993399">0</font><font color="#990000">]</font>
out2<font color="#990000">=</font>cur<font color="#990000">.</font><b><font color="#000000">fetchone</font></b><font color="#990000">()[</font><font color="#993399">1</font><font color="#990000">]</font>
out3<font color="#990000">=</font>cur<font color="#990000">.</font><b><font color="#000000">fetchone</font></b><font color="#990000">()[</font><font color="#993399">2</font><font color="#990000">]</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<p>Here's how you can get multiple values from the output variables
of a MySQL/MariaDB procedure.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font>out <font color="#008080">out1</font> <font color="#009900">int</font><font color="#990000">,</font> out <font color="#008080">out2</font> <font color="#009900">float</font><font color="#990000">,</font> out <font color="#008080">out3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> begin select <font color="#993399">1</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#FF0000">'hello'</font> <font color="#008080">into</font> out1<font color="#990000">,</font> out2<font color="#990000">,</font> out3<font color="#990000">;</font> end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'set @out1=0, @out2=0.0, @out3=</font><font color="#CC33CC">\'\'</font><font color="#FF0000">'</font><font color="#990000">)</font>
cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'call examplefunc(@out1,@out2,@out3)'</font><font color="#990000">)</font>
cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'select @out, @out2, @out3'</font><font color="#990000">)</font>
out1<font color="#990000">=</font>cur<font color="#990000">.</font><b><font color="#000000">fetchone</font></b><font color="#990000">()[</font><font color="#993399">0</font><font color="#990000">]</font>
out2<font color="#990000">=</font>cur<font color="#990000">.</font><b><font color="#000000">fetchone</font></b><font color="#990000">()[</font><font color="#993399">1</font><font color="#990000">]</font>
out3<font color="#990000">=</font>cur<font color="#990000">.</font><b><font color="#000000">fetchone</font></b><font color="#990000">()[</font><font color="#993399">2</font><font color="#990000">]</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<br/><h3>Result Sets</h3>

<p>Some stored procedures return entire result sets.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<h4>Oracle</h4>

<p>Stored procedures in Oracle can return open cursors as return values or output parameters. A client-side cursor can be bound to this open cursor and rows can be fetched from it. However, the SQL Relay Python DB-API driver does not currently support output parameters.</p>

<h4>Sybase and Microsoft SQL Server</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create procedure exampleproc <font color="#008080">as</font> select <font color="#990000">*</font> from exampletable
</tt></pre>

</blockquote>
<p>To exceute the stored procedure from an SQL Relay program, ue code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'call exampleproc'</font><font color="#990000">)</font>
result<font color="#990000">=</font>cur<font color="#990000">.</font><b><font color="#000000">fetchall</font></b><font color="#990000">()</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<br/><h4>Firebird</h4>

<p>Stored procedures in Firebird can return a result set if a
select query in the procedure selects values into the output parameters and
then issues a suspend command, however SQL Relay doesn't currently support
stored procedures that return result sets.</p>

<h4>DB2</h4>

<p>Stored procedures in DB2 can return a result set if the procedure is declared
to return one, however SQL Relay doesn't currently support stored procedures
that return result sets.</p>

<h4>Postgresql</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">function</font> <b><font color="#000000">examplefunc</font></b><font color="#990000">()</font> returns setof <font color="#008080">record</font> as <font color="#FF0000">'</font>
<font color="#FF0000">        declare output record;</font>
<font color="#FF0000">begin</font>
<font color="#FF0000">        for output in select * from mytable loop</font>
<font color="#FF0000">                return next output;</font>
<font color="#FF0000">        end loop;</font>
<font color="#FF0000">        return;</font>
<font color="#FF0000">end;</font>
<font color="#FF0000">'</font> language plpgsql
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'select * from examplefunc() as (col1 int, col2 float, col3 char(20))'</font><font color="#990000">,</font><font color="#FF0000">{</font><font color="#FF0000">'in1'</font><font color="#990000">:</font><font color="#993399">1</font><font color="#990000">,</font><font color="#FF0000">'in2'</font><font color="#990000">:</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#FF0000">'in3'</font><font color="#990000">:</font><font color="#FF0000">'hello'</font><font color="#FF0000">}</font><font color="#990000">)</font>
result<font color="#990000">=</font>cur<font color="#990000">.</font><b><font color="#000000">fetchall</font></b><font color="#990000">()</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop function examplefunc
</tt></pre>

</blockquote>
<h4>MySQL/MariaDB</h4>

<p>The result sets of all select statements called within MySQL/MariaDB stored
procedures (that aren't selected into variables) are returned from the procedure
call.  Though MySQL/MariaDB stored procedures can return multiple result sets,
currently SQL Relay can only fetch the first result set.</p>

<p>To create the stored procedure which returns a result set, run a query like
the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">()</font> <font color="#008080">begin</font> select <font color="#990000">*</font> <font color="#008080">from</font> mytable<font color="#990000">;</font> end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>cur<font color="#990000">.</font><b><font color="#000000">execute</font></b><font color="#990000">(</font><font color="#FF0000">'call examplefunc()'</font><font color="#990000">,</font><font color="#FF0000">{</font><font color="#FF0000">'1'</font><font color="#990000">:</font><font color="#993399">1</font><font color="#990000">,</font><font color="#FF0000">'2'</font><font color="#990000">:</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#FF0000">'3'</font><font color="#990000">:</font><font color="#FF0000">'hello'</font><font color="#FF0000">}</font><font color="#990000">)</font>
result<font color="#990000">=</font>cur<font color="#990000">.</font><b><font color="#000000">fetchall</font></b><font color="#990000">()</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<br/><a name="lastinsertid"/><h2>Getting the Last Insert ID</h2>

<p>Databases with autoincrement or identity columns often provide functions which return the "last insert id"; the value of the autoincrement column that was generated during the insert into the database.</p>

<p>Unfortunately Python DB-API doesn't expose a generic method for getting the last insert id, but when using the SQLite database, you can get the last insert id by running the query:</p>

<blockquote>
<b>select last insert rowid</b>
</blockquote>
</body>
</html>
